<template>
    <div id="app">
        <div v-if="!isAuthing">
            <div class="app-container">
                <!-- 顶部导航栏：仅登录且在/main页面时显示 -->
                <nav class="top-nav" v-if="showSidebar">
                    <router-link to="/main" class="nav-link">主页</router-link>
                    <router-link to="/main/announcements" class="nav-link">校园公告</router-link>
                    <router-link to="/main/second-hand" class="nav-link">二手平台</router-link>
                    <router-link to="/main/forum" class="nav-link">生活论坛</router-link>
                    <router-link to="/main/lunch-draw" class="nav-link">午餐抽签</router-link>
                    <router-link to="/main/ai-qa" class="nav-link">AI问答</router-link>
                    <router-link to="/main/friends" class="nav-link">好友通讯</router-link>
                    <router-link to="/main/profile" class="nav-link">个人中心</router-link>
                    <button v-if="showSidebar" class="logout-btn" @click="logout" style="margin-left:auto;">退出登录</button>
                </nav>
                <!-- 主内容区域 -->
                <main class="main-content topnav-mode">
                    <router-view :key="$route.fullPath"></router-view>
                </main>
            </div>
        </div>
        <div v-else class="app-loading"></div>
    </div>
</template>

<script>
import { ref, computed, watch } from 'vue';
import { useRoute } from 'vue-router';
export default {
    name: 'App',
    setup() {
        const isAuthing = ref(true);
        const route = useRoute();
        const token = ref(localStorage.getItem('token'));
        // 监听token变化（如登录/退出）
        window.addEventListener('storage', () => {
            token.value = localStorage.getItem('token');
        });
        // 登录/退出时主动更新token
        window.__setToken = (val) => { token.value = val };
        // 侧边栏显示条件
        const showSidebar = computed(() => {
            return !!token.value && route.path.startsWith('/main');
        });
        // 监听路由变化，确保token变化后侧边栏立即响应
        watch(route, () => {
            token.value = localStorage.getItem('token');
        });
        // 鉴权loading控制
        window.__setAuthing = (val) => { isAuthing.value = val };
        function logout() {
            if (confirm('您确定要退出登录吗？')) {
                localStorage.removeItem('token');
                window.location.href = '/login';
            }
        }
        return { isAuthing, showSidebar, $route: route, logout };
    }
}
</script>

<style lang="scss">
#app {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: #2c3e50;
    min-height: 100vh;
}

.app-container {
    display: block;
    max-width: 1800px;
    margin: 0 auto;
    padding: 0;
}

.top-nav {
    width: 100%;
    height: 60px;
    display: flex;
    align-items: center;
    background: #f8f9fa;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.08);
    padding: 0 32px;
    gap: 18px;
    margin-bottom: 18px;
}

.nav-link {
    color: #2c3e50;
    text-decoration: none;
    padding: 8px 18px;
    border-radius: 4px;
    font-size: 1.08em;
    transition: background-color 0.3s, color 0.3s;
    background: transparent;
}

.nav-link.router-link-exact-active {
    background-color: #1989fa;
    color: white;
}

.nav-link.router-link-active:not(.router-link-exact-active) {
    background-color: transparent;
    color: #2563eb;
}

.nav-link:hover {
    background-color: #e3e3e3;
}

.main-content.topnav-mode {
    flex: none;
    padding: 1rem 2rem 2rem 2rem;
}

.app-loading {
    min-height: 100vh;
    background: #f8fafc;
}

.logout-btn {
    background: #fff;
    color: #2563eb;
    border: 1px solid #2563eb;
    border-radius: 6px;
    padding: 8px 24px;
    font-size: 1em;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
    display: flex;
    align-items: center;
    gap: 8px;
    margin-left: auto;
}
.logout-btn:hover {
    background: #2563eb;
    color: #fff;
}
</style>